package com.code.thread;

import java.util.concurrent.*;

/**
 * @description: 实现Callable接口开启线程
 * @author: lwh
 * @date: Created in 2020/3/17 11:33
 * @modified By:
 * 总结：会产生缓存，效率提高
 *      结果可能需要等得，会阻塞
 */

public class ThreadDemo03 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        VideoCallable callable = new VideoCallable();
        FutureTask futureTask = new FutureTask(callable);

        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start();//结果会被缓存，所以没有输出，效率高

        Integer o = (Integer) futureTask.get();//这个方法可能会产生阻塞，所以放到最后，或者异步通信处理
        System.out.println(o);
    }
}

class VideoCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"->call()被调用了");

//        加入等待，可以看出结果需要等待输出
        TimeUnit.SECONDS.sleep(5);
        return 200;
    }
}